package com.hxl.alert.config;

import com.hxl.alert.constant.CacheConstants;
import com.hxl.alert.utils.AlertTimeUtils;
import com.hxl.alert.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 在应用启动后立马执行积压消息键值对删除
 */
@Component
public class RedisKeyCleaner implements ApplicationRunner {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void run(ApplicationArguments args) {
        try {
            redisUtils.delete(CacheConstants.UNCONSUMED_KEY);
            logger.info("{} 存储消费积压标识的KEY已清理", AlertTimeUtils.getSdf().format(new Date()));
        } catch (Exception e) {
            logger.error("存储消费积压标识的KEY清理失败:{}", e.getMessage());
        }
    }
}